package com.example.smartwaterapi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartwaterapi.entity.MenstrualCycle;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;

/**
 * 经期管理Mapper接口
 */
@Mapper
public interface MenstrualCycleMapper extends BaseMapper<MenstrualCycle> {
    
    /**
     * 获取用户的经期管理数据
     */
    @Select("SELECT * FROM menstrual_cycle WHERE user_id = #{userId}")
    MenstrualCycle getUserMenstrualCycle(@Param("userId") Long userId);
    
    /**
     * 更新用户经期开始日期
     */
    @Update("UPDATE menstrual_cycle SET last_period_start = #{startDate}, " +
            "next_period_predicted = DATE_ADD(#{startDate}, INTERVAL cycle_length DAY), " +
            "updated_at = NOW() WHERE user_id = #{userId}")
    int updatePeriodStartDate(@Param("userId") Long userId, @Param("startDate") LocalDate startDate);
    
    /**
     * 更新当前周期阶段
     */
    @Update("UPDATE menstrual_cycle SET current_phase = #{phase}, " +
            "current_cycle_day = #{cycleDay}, updated_at = NOW() " +
            "WHERE user_id = #{userId}")
    int updateCurrentPhase(@Param("userId") Long userId, @Param("phase") String phase, @Param("cycleDay") Integer cycleDay);
    
    /**
     * 更新经期周期设置
     */
    @Update("UPDATE menstrual_cycle SET cycle_length = #{cycleLength}, " +
            "period_length = #{periodLength}, " +
            "next_period_predicted = DATE_ADD(last_period_start, INTERVAL #{cycleLength} DAY), " +
            "updated_at = NOW() WHERE user_id = #{userId}")
    int updateCycleSettings(@Param("userId") Long userId, @Param("cycleLength") Integer cycleLength, @Param("periodLength") Integer periodLength);
    
    /**
     * 更新饮水建议量
     */
    @Update("UPDATE menstrual_cycle SET " +
            "menstrual_water_goal = #{menstrualGoal}, " +
            "follicular_water_goal = #{follicularGoal}, " +
            "ovulation_water_goal = #{ovulationGoal}, " +
            "luteal_water_goal = #{lutealGoal}, " +
            "updated_at = NOW() WHERE user_id = #{userId}")
    int updateWaterGoals(@Param("userId") Long userId, 
                        @Param("menstrualGoal") Integer menstrualGoal, 
                        @Param("follicularGoal") Integer follicularGoal, 
                        @Param("ovulationGoal") Integer ovulationGoal, 
                        @Param("lutealGoal") Integer lutealGoal);
} 